<template>
  <div>
    <a-select
      mode="multiple"
      style="width: 100%"
      placeholder="请选择印章"
      @deselect="deselect"
      v-model="ids"
      :open="open"
      :disabled="disabled"
      @dropdownVisibleChange="selectIds">
      <a-select-option v-for="item in selectedObj" :key="item.id">{{ item.sealType_dictText?item.sealType_dictText: item.sealType }}</a-select-option>
    </a-select>
    <select-normal-seal-input-model ref="inputModal" :urlList="url.urlList" :multi="true" :company="company" @choseObj="choseObj"></select-normal-seal-input-model>
    <select-erp-seal-certificate-file-manage-list ref="manageList"
                                                  @cCopy="copyData"></select-erp-seal-certificate-file-manage-list>
  </div>
</template>

<script>
  import SelectNormalSealInputModel from './SelectNormalSealInputModel'
  import SelectErpSealCertificateFileManageList from '../SelectErpSealCertificateFileManageList'
  import { getAction } from '@/api/manage'

  export default {
    name: 'SelectNormalSealInput',
    components: { SelectNormalSealInputModel, SelectErpSealCertificateFileManageList },
    props: {
      disabled: {
        type: Boolean,
        required: false,
        default: false
      },
      open: {
        type: Boolean,
        required: false,
        default: false
      },
      value: {
        type: String,
        required: false
      },
      multi: {
        type: Boolean,
        default: false,
        required: false
      },
      props: {
        type: Object,
        default: null
      },
      company:{
        type: String,
        required: false
      },
    },
    data() {
      return {
        ids: [],
        selectedObj: [],
        deleteIds: [],
        list: [],
        objs: '',
        url: {
          queryById: '/api/erp/administration/seal/erpSealCertificationChangeRecord/queryByIds2',
          queryByIds: '/api/erp/administration/seal/erpSealCertificationChangeRecord/queryByIds2',
          urlList: '/api/erp/administration/seal/erpSealCertificateFileManage/list1'
        },
        dictOptions: {},
        sealList: [],
      }
    },
    mounted() {
      // debugger;
      this.initObj(this.value);
    },
    watch: {
      value(val) {
        this.initObj(val);
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    methods: {
      copyData(data) {
        console.log('copyData', data);
        this.$emit('ccCopy', data);
      },
      initObj(ids) {
        this.selectedObj = [];
        if (ids && ids.length > 0) {
          this.ids = ids.split(',');
          let url = '';
          let param = {};
          url = this.url.queryByIds;
          param = { ids: ids };
          getAction(url, param).then((res) => {
            if (res.success) {
              if (!Array.isArray(res.result)) {
                this.selectedObj.push(res.result);
              } else {
                for (let i = 0; i < res.result.length; i++) {
                  this.selectedObj.push(res.result[i]);
                }
              }
              this.$refs.inputModal.edit(res.result, this.ids);
              this.$refs.manageList.sealList = this.selectedObj;
            }
          });
        } else {
          this.ids = [];
          this.$refs.inputModal.edit(null, null);
        }
      },
      // 选择的数据
      choiceObjList(ids) {
        var arr = []
        if (ids && ids.length > 0) {
          this.ids = ids.split(',');
          let url = '';
          let param = {};
          url = this.url.queryByIds;
          param = { ids: ids };
          getAction(url, param).then((res) => {
            if (res.success) {
              if (!Array.isArray(res.result)) {
                arr.push(res.result);
              } else {
                for (let i = 0; i < res.result.length; i++) {
                  arr.push(res.result[i]);
                }
              }
            }
          });
          return arr
        }
      },
      resetObj() {
        this.userType = false;
        this.ids = [];
        this.selectedObj = [];
        this.deleteIds = [];
        this.open = false;
        this.$refs.inputModal.edit(null, null);
        this.$refs.manageList.sealList = []
      },
      selectIds() {
        if (!this.disabled) {
          this.deleteId();
          this.$refs.inputModal.add(this.selectedObj, this.ids);
        }
      },
      deselect(value, option) {
        this.deleteIds.push(value);
        this.deleteId();
        this.choseObj(this.selectedObj);
      },
      deleteId() {
        let that = this;
        this.selectedObj = this.selectedObj.filter(function (e) {
          return that.deleteIds.indexOf(e.id) == -1;
        })
        this.deleteIds = [];
      },
      // 子modal回调
      choseObj: function (list) {
        this.ids = [];
        for (var i = 0; i < list.length; i++) {
          this.ids.push(list[i].id);
        }
        var ids = this.ids.toString()
        this.selectedObj = this.choiceObjList(ids);
        this.objs = this.ids.join(',');
        this.$emit('change', this.objs);
        this.$emit('back', this.selectedObj);
        this.$refs.manageList.sealList = this.selectedObj;
      }
    }
  }
</script>

<style scoped>

</style>
